package com.yylending.las.authority.password;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IEvent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.AESEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by HenryZhang on 2016/11/20.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class ModifyPwdPresenter extends Presenter<ModifyPwdModel, ModifyPwdPresenter.View> {

	private final static String TAG = "ModifyPwdPresenter";

	public static class Generator implements PresenterFactory<ModifyPwdPresenter> {
		@Override
		public ModifyPwdPresenter create() {
			return new ModifyPwdPresenter();
		}
	}

	public interface View extends IView {
		void onResetPwdSuccess();

		void onResetPwdFailure(String msg);

		void onResetPwdRetCode(String retCode);
	}

	public static class Event extends IEvent {
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new ModifyPwdModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void modifyResult(ModifyPwdModel.ModifyPwdResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				mView.onResetPwdSuccess();
			} else {
				String msg = result.getRetMsg();
				mView.onResetPwdFailure(TextUtils.isEmpty(msg) ? "修改密码失败" : msg);
			}
			mView.onResetPwdRetCode(result.getRetCode());
		}
	}

	public void modify(String oldPwd, String newPwd) {
		execute(new AESEntity(new ModifyPwdModel.ModifyPwd(oldPwd, newPwd)), FUNCTIONAL_CODE, true, null, mRequestPage);
	}
}
